//
//  SINAStuActivityAddTargetInputViewController.m
//  sinashebao
//
//  Created by  androidlongs on 2018/1/24.
//  Copyright © 2018年  androidlongs. All rights reserved.
//

#import "SINAStuMyAlertNikNameController.h"

@interface SINAStuMyAlertNikNameController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *inputTargerField;
@property (weak, nonatomic) IBOutlet UILabel *textNumberFlagLabel;

@end

@implementation SINAStuMyAlertNikNameController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title=@"修改昵称";
    
    [super initBackItem];
    UIView *rightView =[[UIView alloc]initWithFrame:CGRectMake(0,0,40,50)];
    UIButton *rightButton =[[UIButton alloc]initWithFrame:CGRectMake(0,0,40,45)];
    [rightButton addTarget:self action:@selector(saveDataFunction) forControlEvents:UIControlEventTouchUpInside];
    [rightView addSubview:rightButton];
    [rightButton setTitle:@"保存" forState:UIControlStateNormal];
    UIBarButtonItem *rightButtonView = [[UIBarButtonItem alloc]initWithCustomView:rightView ];
    self.navigationItem.rightBarButtonItem=rightButtonView;
    
    [self.inputTargerField addTarget:self action:@selector(searchTextFieldChangeAction:)   forControlEvents:UIControlEventEditingChanged];
    
    
    self.inputTargerField.tag=100;
    self.inputTargerField.delegate=self;
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[NSForegroundColorAttributeName] = [UIColor colorWithHexString:@"#DDDDDD"];
    NSAttributedString *attribute = [[NSAttributedString alloc] initWithString:@"请输入昵称" attributes:dict];
    [self.inputTargerField setAttributedPlaceholder:attribute];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)searchTextFieldChangeAction: (id)sender
{
    NSInteger textInteger =10-[self.inputTargerField.text length];
    
    self.textNumberFlagLabel.text=[NSString stringWithFormat:@"还可以输入%ld字",(long)textInteger];
    
}
#pragma mark -- 设定可输入10位数
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    NSInteger maxLength = 10;//设置限制字数
    
    if (textField.tag == 100) {
        NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
        
        if (toBeString.length > maxLength && range.length!=1){
            textField.text = [toBeString substringToIndex:maxLength];
            
            return NO;
            
        }
        
    }
    
    return YES;
}


//提交保存数据
-(void)saveDataFunction{
    
    [self.view endEditing:YES];
    
    NSString *userSign = self.inputTargerField.text;
    
    if (userSign==nil||userSign.length<1) {
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"请输入昵称" sureBtn:@"确定" cancleBtn:nil];
        [alertView showXLAlertView];
        return;
    }
    
    SINACommonUserHelper *helper =[SINACommonUserHelper share];
    SINAUserModel *userModel = [helper getUserModel];
    
    if (userModel!=nil&&userModel.userUid!=nil&&![userModel.userUid isEqualToString:@""]) {

        [self startLoading];
        NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
        [dict setObject:userModel.userUid forKey:@"uuid"];
        [dict setObject:userSign forKey:@"nickname"];
        NSMutableDictionary *dict2 = [[NSMutableDictionary alloc]init];
        [dict2 setObject:[SINAHttpRequestUtils encodeDicParams:dict] forKey:@"sj"];
        WeakSelf;
        [CXAFNHttpRequestUtils httpPostRequest:[NSString stringWithFormat:@"%@%@",baseUrl,userAlertInformationUrl] withParamters:dict2 success:^(id responseData) {
            NSString *code = responseData[@"code"];
            
            [weakSelf stopLoading];
            
            if ([code isEqualToString:@"1000"]) {
//                self.loadingView.text=@"修改成功";
                
                [YPXToastView showToastViewWithText:@"修改成功" andDuration:2 andCorner:5 andParentView:weakSelf.view];
                
                SINACommonUserHelper *helper =[SINACommonUserHelper share];
                SINAUserModel *userModel = [helper getUserModel];
                userModel.userNike = userSign;
                [helper saveUserModel:userModel];
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [weakSelf stopLoading];
                    [weakSelf.navigationController popViewControllerAnimated:YES];
                    
                });
            }else{
                [weakSelf stopLoading];
                NSString *message =responseData[@"message"];
                WISAlertView *alertView =  [[WISAlertView alloc]initWithTitle:@"提示" message:message sureBtn:@"确定" cancleBtn:nil];
                [alertView showXLAlertView];
            }
            
        } failure:^(NSError *error, NSInteger errCode) {
            DLog(@"加载 失败  %@ ",error.debugDescription);
            [weakSelf stopLoading];
            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"验证失败" sureBtn:@"确定" cancleBtn:nil];
            [alertView showXLAlertView];
        }];
    }else{
        
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"用户信息异常请重新进入" sureBtn:@"确定"
                                                             cancleBtn:nil];
        [alertView showXLAlertView];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
